package br.com.serialmanager.util;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	
	private static String DB_PATH = "/data/data/br.com.serialmanager/databases/";
	private static String DB_NAME = "serialmanager.db";
	private SQLiteDatabase dbQuery;
	private final Context dbContexto;

	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null, 1);
		this.dbContexto = context;
		try {
			this.criarDatabase();
		} catch (IOException e) {
			Log.e("ERRO_CRIAR_BANCO_DE_DADOS", e.getMessage());
		}
		this.abrirDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	public void criarDatabase() throws IOException {
		boolean dbExist = this.checkDatabase();
		if (!dbExist) {
			this.getReadableDatabase();
			try {
				this.copiarDatabase();
			} catch (IOException e) {
				throw new Error("Erro ao copiar o banco de dados!");
			}
		}
	}

	private void copiarDatabase() throws IOException {
		// TODO Auto-generated method stub
		InputStream myInput = this.dbContexto.getAssets().open(DB_NAME);
		String outFileName = DB_PATH + DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);
		
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}
		
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	private boolean checkDatabase() {
		// TODO Auto-generated method stub
		SQLiteDatabase checkDb = null;
		try {
			String myPath = DB_PATH + DB_NAME;
			checkDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
		} catch (SQLiteException e) {
			System.out.println("Erro: " + e.getMessage());
		}
		
		if (checkDb != null) {
			checkDb.close();
		}
		return checkDb != null ? true : false;
	}

	public void abrirDatabase() throws SQLException {
		String myPath = DB_PATH + DB_NAME;
		this.dbQuery = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
	}

	@Override
	public synchronized void close() {
		// TODO Auto-generated method stub
		if (this.dbQuery != null) {
			this.dbQuery.close();
		}
		super.close();
	}
	
	public static class Software {
		public static final String TABELA = "tb_software";
		public static final String _ID = "soft_id";
		public static final String NOME = "soft_nome";
		public static final String DATA_VALIDADE = "soft_validade";
		public static final String NUMERO_DE_SERIE = "soft_nserie";
		public static final String MASCARA_ID = "soft_masc_id";
		public static final String[] COLUNAS = {_ID, NOME, DATA_VALIDADE, NUMERO_DE_SERIE, MASCARA_ID};
	}
	
	public static class Mascara {
		public static final String TABELA = "tb_mascara";
		public static final String _ID = "masc_id";
		public static final String FORMATO = "masc_formato";
		public static final String[] COLUNAS = {_ID, FORMATO};
	}
}
